package com.htrq.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.htrq.dao.PoundDao;
import com.htrq.entity.Page;
import com.htrq.entity.PoundData;
@Service
@Transactional(value="txManager")
public class PoundService {
	@Resource
	private PoundDao pDao;

	public String checkandsave(String[][] result) throws ParseException {
		// TODO Auto-generated method stub
		String str = "";
		// 检查excel文件第二行第一列是否是流水号，判断是否是磅单文件
		if (result[0][0].matches("^[A-Z]{1}\\d{12}$")) {
			// 保存磅单数据
			for (String[] ary : result) {
				PoundData pod = new PoundData();
				pod.setAutoId(ary[0]);
				pod.setCarNum(ary[1]);
				pod.setSendcom(ary[2]);
				pod.setAccept(ary[3]);
				pod.setHname(ary[4]);
				pod.setYtype(ary[5]);
				pod.setPweight(Integer.parseInt(ary[6]));
				pod.setMweight(Integer.parseInt(ary[7]));
				pod.setJweight(Integer.parseInt(ary[8]));
				pod.setmDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(ary[9]));
				pod.setpDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(ary[10]));
				pod.setGcarNum(ary[11]);
				// 开始验证数据重复性 不重复为true，重复为false
				boolean flag = pDao.contains(pod);
				if (flag) {
					pDao.add(pod);
				} else {
					str += "有数据重复:" + pod.getAutoId()+"-";
				}
			}
			str += "保存成功";
		} else {
			str = "请检查是否是磅单数据excel";
		}
		return str;
	}
	public List<PoundData> getAllpod(PoundData pod, Page page) {
		//TODO Auto-generated method stub
		return pDao.getAllpod(pod,page);
	}
	public Long getTotal(PoundData pod) {
		//TODO Auto-generated method stub
		return pDao.getTotal(pod);
	}
	public boolean modify(PoundData pod) {
		//TODO Auto-generated method stub
		try {
			pDao.modify(pod);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public List<Object[]> exportbyForm(PoundData pod) {
		//TODO Auto-generated method stub
		return pDao.exportbyForm(pod);
	}
}
